<template>
    <n-form
        ref="formRef"
        label-placement="top"
        :label-width="80"
        :model="form"
        :rules="rules"
    >
        <n-form-item label="敏感词" path="content">
            <n-input v-model:value="form.content" placeholder="请输入敏感词" />
        </n-form-item>
    </n-form>
</template>
<script setup lang="ts">
import { ref } from 'vue'
import type { FormInst, FormRules } from 'naive-ui'
import { Rules } from '@/utils'
import { JBoltApi } from '@/utils/request'
import { useResetableData } from '@/hooks/common/use-resetable-data'

const props = withDefaults(
    defineProps<{
        id: string
        content: string
    }>(),
    {
        id: '',
        content: ''
    }
)

/** 表单相关 start */
const formRef = ref<FormInst | null>()
interface DictionaryType {
    id: string
    content: string
}
const form = useResetableData<DictionaryType>({
    id: props.id ?? '',
    content: props.content ?? ''
})
const rules: FormRules = {
    content: new Rules().required('请输入名称').value
}

/**
 * 提交表单
 */
async function submit() {
    await formRef.value?.validate()

    let url = props.id
        ? '/api/admin/sensitiveWord/update'
        : '/api/admin/sensitiveWord/save'
    await JBoltApi.tryPost(url, form)
    return true
}

defineExpose({
    submit
})

/** 表单相关 end */
</script>
<style scoped></style>
